; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --include-generated-funcs
; RUN: opt < %s -passes='cgscc(coro-split),simplifycfg,early-cse,simplifycfg,coro-cleanup' -S | FileCheck %s

define ptr @f(ptr %buffer, i32 %n) presplitcoroutine {
entry:
  %id = call token @llvm.coro.id.retcon(i32 8, i32 4, ptr %buffer, ptr @prototype, ptr @allocate, ptr @deallocate)
  %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
  %value0 = call i32 (...) @llvm.coro.suspend.retcon.i32()
  %sum0 = call i32 @add(i32 %n, i32 %value0)
  %value1 = call i32 (...) @llvm.coro.suspend.retcon.i32()
  %sum1 = call i32 @add(i32 %sum0, i32 %value0)
  %sum2 = call i32 @add(i32 %sum1, i32 %value1)
  %value2 = call i32 (...) @llvm.coro.suspend.retcon.i32()
  %sum3 = call i32 @add(i32 %sum2, i32 %value0)
  %sum4 = call i32 @add(i32 %sum3, i32 %value1)
  %sum5 = call i32 @add(i32 %sum4, i32 %value2)
  call void @print(i32 %sum5)
  call i1 @llvm.coro.end(ptr %hdl, i1 0)
  unreachable
}

declare token @llvm.coro.id.retcon(i32, i32, ptr, ptr, ptr, ptr)
declare ptr @llvm.coro.begin(token, ptr)
declare i32 @llvm.coro.suspend.retcon.i32(...)
declare i1 @llvm.coro.end(ptr, i1)
declare ptr @llvm.coro.prepare.retcon(ptr)

declare ptr @prototype(ptr, i32)

declare noalias ptr @allocate(i32 %size)
declare void @deallocate(ptr %ptr)

declare i32 @add(i32, i32)
declare void @print(i32)

; CHECK-LABEL: @f(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call ptr @allocate(i32 20)
; CHECK-NEXT:    store ptr [[TMP0]], ptr [[BUFFER:%.*]], align 8
; CHECK-NEXT:    store i32 [[N:%.*]], ptr [[TMP0]], align 4
; CHECK-NEXT:    ret ptr @f.resume.0
;
;
; CHECK-LABEL: @f.resume.0(
; CHECK-NEXT:  entryresume.0:
; CHECK-NEXT:    [[TMP2:%.*]] = load ptr, ptr [[TMP0:%.*]], align 8
; CHECK-NEXT:    [[VALUE0_SPILL_ADDR:%.*]] = getelementptr inbounds [[F_FRAME:%.*]], ptr [[TMP2]], i32 0, i32 1
; CHECK-NEXT:    store i32 [[TMP1:%.*]], ptr [[VALUE0_SPILL_ADDR]], align 4
; CHECK-NEXT:    [[N_RELOAD:%.*]] = load i32, ptr [[TMP2]], align 4
; CHECK-NEXT:    [[SUM0:%.*]] = call i32 @add(i32 [[N_RELOAD]], i32 [[TMP1]])
; CHECK-NEXT:    [[SUM0_SPILL_ADDR:%.*]] = getelementptr inbounds [[F_FRAME]], ptr [[TMP2]], i32 0, i32 2
; CHECK-NEXT:    store i32 [[SUM0]], ptr [[SUM0_SPILL_ADDR]], align 4
; CHECK-NEXT:    ret ptr @f.resume.1
;
;
; CHECK-LABEL: @f.resume.1(
; CHECK-NEXT:  entryresume.1:
; CHECK-NEXT:    [[TMP2:%.*]] = load ptr, ptr [[TMP0:%.*]], align 8
; CHECK-NEXT:    [[VALUE1_SPILL_ADDR:%.*]] = getelementptr inbounds [[F_FRAME:%.*]], ptr [[TMP2]], i32 0, i32 3
; CHECK-NEXT:    store i32 [[TMP1:%.*]], ptr [[VALUE1_SPILL_ADDR]], align 4
; CHECK-NEXT:    [[SUM0_RELOAD_ADDR:%.*]] = getelementptr inbounds [[F_FRAME]], ptr [[TMP2]], i32 0, i32 2
; CHECK-NEXT:    [[SUM0_RELOAD:%.*]] = load i32, ptr [[SUM0_RELOAD_ADDR]], align 4
; CHECK-NEXT:    [[VALUE0_RELOAD_ADDR5:%.*]] = getelementptr inbounds [[F_FRAME]], ptr [[TMP2]], i32 0, i32 1
; CHECK-NEXT:    [[VALUE0_RELOAD6:%.*]] = load i32, ptr [[VALUE0_RELOAD_ADDR5]], align 4
; CHECK-NEXT:    [[SUM1:%.*]] = call i32 @add(i32 [[SUM0_RELOAD]], i32 [[VALUE0_RELOAD6]])
; CHECK-NEXT:    [[SUM2:%.*]] = call i32 @add(i32 [[SUM1]], i32 [[TMP1]])
; CHECK-NEXT:    [[SUM2_SPILL_ADDR:%.*]] = getelementptr inbounds [[F_FRAME]], ptr [[TMP2]], i32 0, i32 4
; CHECK-NEXT:    store i32 [[SUM2]], ptr [[SUM2_SPILL_ADDR]], align 4
; CHECK-NEXT:    ret ptr @f.resume.2
;
;
; CHECK-LABEL: @f.resume.2(
; CHECK-NEXT:  entryresume.2:
; CHECK-NEXT:    [[TMP2:%.*]] = load ptr, ptr [[TMP0:%.*]], align 8
; CHECK-NEXT:    [[SUM2_RELOAD_ADDR:%.*]] = getelementptr inbounds [[F_FRAME:%.*]], ptr [[TMP2]], i32 0, i32 4
; CHECK-NEXT:    [[SUM2_RELOAD:%.*]] = load i32, ptr [[SUM2_RELOAD_ADDR]], align 4
; CHECK-NEXT:    [[VALUE1_RELOAD_ADDR:%.*]] = getelementptr inbounds [[F_FRAME]], ptr [[TMP2]], i32 0, i32 3
; CHECK-NEXT:    [[VALUE1_RELOAD:%.*]] = load i32, ptr [[VALUE1_RELOAD_ADDR]], align 4
; CHECK-NEXT:    [[VALUE0_RELOAD_ADDR:%.*]] = getelementptr inbounds [[F_FRAME]], ptr [[TMP2]], i32 0, i32 1
; CHECK-NEXT:    [[VALUE0_RELOAD:%.*]] = load i32, ptr [[VALUE0_RELOAD_ADDR]], align 4
; CHECK-NEXT:    [[SUM3:%.*]] = call i32 @add(i32 [[SUM2_RELOAD]], i32 [[VALUE0_RELOAD]])
; CHECK-NEXT:    [[SUM4:%.*]] = call i32 @add(i32 [[SUM3]], i32 [[VALUE1_RELOAD]])
; CHECK-NEXT:    [[SUM5:%.*]] = call i32 @add(i32 [[SUM4]], i32 [[TMP1:%.*]])
; CHECK-NEXT:    call void @print(i32 [[SUM5]])
; CHECK-NEXT:    call void @deallocate(ptr [[TMP2]])
; CHECK-NEXT:    ret ptr null
;
